package one.stand.controller;

import com.alibaba.fastjson.JSON;
import one.stand.request.ComponentProgramRequest;
import one.stand.request.ComponentVerifyTicketRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.Map;

/**
 * @author: QIK
 * @CreateDate: 2019/5/9 14:33
 */
@RestController
@RequestMapping("wechatApi/wx")
public class WxController extends BaseController {

    @RequestMapping("wxcallback/{appid}")
    public String wxcallback(@PathVariable("appid") String appid, HttpServletRequest httpServletRequest, ComponentVerifyTicketRequest request) {
        Map<String ,String[]> data=httpServletRequest.getParameterMap();
        logger.info("回调参数ticket:{}",JSON.toJSONString(data));
        StringBuffer inputString = new StringBuffer();
        try {
            BufferedReader reader = httpServletRequest.getReader();
            String line;
            while ((line = reader.readLine()) != null) {
                inputString.append(line);
            }
            httpServletRequest.getReader().close();
            logger.info("微信回调流ticket：{}",inputString.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAppid(appid);
        request.setXml(inputString.toString());
        return  wechatFegin.wxcallback(request);
    }


    @RequestMapping("program/{appid}")
    public String program(@PathVariable("appid") String appid, HttpServletRequest httpServletRequest, ComponentProgramRequest request) {
        Map<String ,String[]> data=httpServletRequest.getParameterMap();
        logger.info("回调参数program:{}",JSON.toJSONString(data));
        StringBuffer inputString = new StringBuffer();
        try {
            BufferedReader reader = httpServletRequest.getReader();
            String line;
            while ((line = reader.readLine()) != null) {
                inputString.append(line);
            }
            httpServletRequest.getReader().close();
            logger.info("微信回调流program：{}",inputString.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAppid(appid);
        request.setXml(inputString.toString());
        return  wechatFegin.program(request);
    }
}
